//
//  FeedBackViewController.m
//  Funny
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 King. All rights reserved.
//

#import "FeedBackViewController.h"
#import <MessageUI/MessageUI.h>

@interface FeedBackViewController ()<MFMailComposeViewControllerDelegate>

@property (nonatomic, strong) UITextField *e_mailTextField;
@property (nonatomic, strong) UITextView *textView;

@end

@implementation FeedBackViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.e_mailTextField];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(20, 60, ScreenWidth - 40, 30)];
    lable.text = @"请留下您宝贵的意见:";
    lable.textColor = [UIColor grayColor];
    [self.view addSubview:lable];
    
    UIButton *sendClick = [UIButton buttonWithFrame:CGRectMake(20, 250, ScreenWidth - 40, 30) color:nil title:@"发送" type:UIButtonTypeSystem target:self action:@selector(sendEmail) event:UIControlEventTouchUpInside cornerRadius:5.0];
//    sendClick.tintColor = [UIColor whiteColor];
    [self.view addSubview:sendClick];
    
    [self.view addSubview:self.textView];
    // Do any additional setup after loading the view.
}

- (UITextView *)textView
{
    if (!_textView) {
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, ScreenWidth - 40, 150)];
        _textView.layer.borderWidth = 1;
        _textView.layer.borderColor = [UIColor grayColor].CGColor;
        _textView.layer.masksToBounds = YES;
        _textView.layer.cornerRadius = 5;
    }
    return _textView;
}

- (UITextField *)e_mailTextField
{
    if (!_e_mailTextField) {
        self.e_mailTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, ScreenWidth - 40, 30)];
        _e_mailTextField.placeholder = @"请输入您的邮箱账号";
        _e_mailTextField.keyboardType = UIKeyboardTypeEmailAddress;
        _e_mailTextField.layer.borderColor = [UIColor grayColor].CGColor;
        _e_mailTextField.clearButtonMode = UITextFieldViewModeAlways;
        _e_mailTextField.layer.borderWidth = 1;
        _e_mailTextField.layer.masksToBounds = YES;
        _e_mailTextField.layer.cornerRadius = 5;
    }
    return _e_mailTextField;
}

- (void)sendEmail
{
    [self.textView resignFirstResponder];
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        if ([mailClass canSendMail])
        {
            [self displayComposerSheet];
        }
        else
        {
            [self launchMailAppOnDevice];
        }
    }
    else
    {
        [self launchMailAppOnDevice];
    }
}

//可以发送邮件的话
-(void)displayComposerSheet
{
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    mailPicker.mailComposeDelegate = self;
    
    //设置主题
    [mailPicker setSubject: @"意见反馈"];
    
    // 添加发送者
    NSArray *toRecipients = [NSArray arrayWithObject: @"963677621@qq.com"];
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];
    [mailPicker setToRecipients: toRecipients];
    //    [picker setCcRecipients:ccRecipients];
    //    [picker setBccRecipients:bccRecipients];
    
    ////     添加图片
    //    UIImage *addPic = [UIImage imageNamed: @"AppIcon"];
    //    NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    ////     NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg
    //    [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @""];
    NSString *emailBody = _textView.text;
    [mailPicker setMessageBody:emailBody isHTML:YES];
    
    [self presentViewController:mailPicker animated:YES completion:^{
    }];
}

-(void)launchMailAppOnDevice
{
    [self alertWithTitle:@"提示" message:@"请配置您的邮箱:"];
    NSString *recipients = @"362781242@qq.com";
    //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
    NSString *body = self.textView.text;
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    NSString *msg;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            msg = @"邮件发送取消";
            [self alertWithTitle:@"提示" message:msg];
            break;
        case MFMailComposeResultSaved:
            msg = @"邮件保存成功";
            [self alertWithTitle:@"提示" message:msg];
            break;
        case MFMailComposeResultSent:
            msg = @"邮件发送成功";
            [self alertWithTitle:@"提示" message:msg];
            break;
        case MFMailComposeResultFailed:
            msg = @"邮件发送失败";
            [self alertWithTitle:@"提示" message:msg];
            break;
        default:
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void) alertWithTitle:(NSString *)title message:(NSString *)message
{
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:0];
//    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//    }];
//    [alert addAction:okAction];
//    [self presentViewController:alert animated:YES completion:^{
//        
//    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_e_mailTextField resignFirstResponder];
    [_textView resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
